package com.beizhen.demo.controller;

import com.beizhen.demo.entity.StuBaseTeachers;
import com.beizhen.demo.service.StuBaseTeachersService;
import com.beizhen.demo.utils.Fileupload;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
* 基地师资表 前端控制器
*/
@Controller
    public class StuBaseTeachersController {
    @Autowired
    private StuBaseTeachersService stuBaseTeachersService;

    //进入添加师资页面
    @RequestMapping("/addTexml")
    public String addTexml(){
        return "base_administration_teachers_particulars";
    }


    //获取师资信息
    @RequestMapping("/getTeachersList")
    public String  getTeachersList(HttpServletRequest request){
        List<StuBaseTeachers> teachers=stuBaseTeachersService.getTeachersList(1);
        for (StuBaseTeachers s :teachers) {
            System.out.println(s.getBtName());
        }
        request.setAttribute("teacherslist",teachers);
        return "base_administration_teachers";
    }


    //添加师资信息
    @RequestMapping(value="/addTeachers",method = RequestMethod.POST)
    public String addTeachers(StuBaseTeachers teachers , HttpServletRequest request, HttpSession session,
                              @RequestParam(value = "attachs",required =false) MultipartFile attach){
        //目标路径
        String path=request.getSession().getServletContext().getRealPath("static"+ File.separator+"uploadfiles");
        //封面url
        String CoverImgPath="";
        //错误信息
        String errorInfo="";
        if (!attach.isEmpty()){
                //获取原文件名字
                String oldFileName=attach.getOriginalFilename();
                //获取后缀
                String prefix= FilenameUtils.getExtension(oldFileName);
                if (attach.getSize()>500000){
                    System.out.println("上传大小不超过5MB");
                    request.setAttribute("uploadFileError","上传大小不超过5MB");
                    return "base_administration_teachers_particulars";
                }else if (prefix.equalsIgnoreCase("jpg")||
                        prefix.equalsIgnoreCase("png")||
                        prefix.equalsIgnoreCase("jpeg")||
                        prefix.equalsIgnoreCase("pneg")){
                    //写入
                    try {
                        CoverImgPath= Fileupload.getUrl(attach);
                        System.out.println(CoverImgPath);
                        teachers.setBtPicture(CoverImgPath);
                    } catch (IOException e) {
                        e.printStackTrace();
                        request.setAttribute(errorInfo,"上传失败");
                        return "base_enter_audit";
                    }
                }else{
                    System.out.println("上传图片格式不正确");
                    request.setAttribute("uploadFileError","上传图片格式不正确");
                    return "base_administration_teachers_particulars";
                }
            }
        teachers.setBtBaseid(1);
        teachers.setBtDisable(0);
        int i= stuBaseTeachersService.addTeachers(teachers);
        if (i>0){
            System.out.printf("返回结果"+i);
            return "redirect:getTeachersList";
        }
        return "base_administration_teachers_particulars";
    }
    

    //修改师资
    @RequestMapping("/updTeachers")
    public String updTeachers(StuBaseTeachers teachers , HttpServletRequest request, HttpSession session,
                              @RequestParam(value = "attachs",required =false) MultipartFile attach){
        //目标路径
        String path=request.getSession().getServletContext().getRealPath("static"+ File.separator+"uploadfiles");
        //封面url
        String CoverImgPath="";
        //错误信息
        String errorInfo="";
        if (!attach.isEmpty()){
            //获取原文件名字
            String oldFileName=attach.getOriginalFilename();
            //获取后缀
            String prefix= FilenameUtils.getExtension(oldFileName);
            if (attach.getSize()>500000){
                System.out.println("上传大小不超过5MB");
                request.setAttribute("uploadFileError","上传大小不超过5MB");
                return "";
            }else if (prefix.equalsIgnoreCase("jpg")||
                    prefix.equalsIgnoreCase("png")||
                    prefix.equalsIgnoreCase("jpeg")||
                    prefix.equalsIgnoreCase("pneg")){
                //写入
                try {
                    CoverImgPath= com.beizhen.demo.utils.Fileupload.getUrl(attach);
                    System.out.println(CoverImgPath);
                    teachers.setBtPicture(CoverImgPath);
                } catch (IOException e) {
                    e.printStackTrace();
                    request.setAttribute(errorInfo,"上传失败");
                    return "base_enter_audit";
                }
            }else{
                System.out.println("上传图片格式不正确");
                request.setAttribute("uploadFileError","上传图片格式不正确");
                return "";
            }
        }
        //基地外键
        teachers.setBtBaseid(1);
        //发布默认为0 发布
        teachers.setBtDisable(0);
        int i= stuBaseTeachersService.updTeachers(teachers);
        if (i>0){
            System.out.printf("返回结果"+i);
            return "";
        }
        return "";
    }

    //删除师资信息
    @RequestMapping("/clearTeacher")
    public String  clearTeacher(Integer id){
        StuBaseTeachers teachers=stuBaseTeachersService.getTeachers(id);

        if (null!=teachers.getBtPicture()){
            File file=new File(teachers.getBtPicture());
            file.delete();

        }
        stuBaseTeachersService.clearTeachers(id);
        return "redirect:/getTeachersList";
    }

    @RequestMapping("/issueTeacher")
    @ResponseBody
    public String issueTeacher(Integer id){
        StuBaseTeachers teachers=new StuBaseTeachers();
        teachers.setBtId(id);
        teachers.setBtDisable(1);//状态0发布 1不发布
        int i=stuBaseTeachersService.updTeachers(teachers);
        if (i>0){
            System.out.println("发布成工");
        }
        return "";
    }


    /**
     * 获得项目工程的绝对路径
     */
    public static String getWebRoot(HttpServletRequest request) {
        return request.getSession().getServletContext().getRealPath("/");
    }

    /**
     * 获得发布后的jar当前路径
     */
    public String getJarRoot(){
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        return jarFile.getParentFile().getPath();
    }
}